﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATPlatforms.RabbitSkin.Serializers;
using RabbitMQ.Client;

namespace ATPlatforms.RabbitSkin
{
    public class MessageProducer<T> : BaseProducerConsumer
    {
        public ISerializeMessages<T> MessageSerializer { get; set; }

        public MessageProducer(string server, string exchange, string exchangeType) :
            this(server, exchange, exchangeType, new BinaryMessageSerializer<T>())
        {
        }

        public MessageProducer(string server, string exchange, string exchangeType, ISerializeMessages<T> messageSerializer) : 
            base(server, exchange, exchangeType)
        {
            MessageSerializer = messageSerializer;
        }

        public void SendMessage(T message)
        {
            SendMessage(message, "");
        }

        public void SendMessage(T message, string routingKey)
        {
            IBasicProperties basicProperties = Model.CreateBasicProperties();
            try
            {
                Model.BasicPublish(Exchange, routingKey, false, false, basicProperties,
                                   MessageSerializer.SerializeMessage(message));
            }catch(ArgumentNullException ae)
            {
                // message must have been null, do something or nothing? silentfail? 
                //could send to log queue
            }catch(Exception e)
            {
                
            }
        }
    }
}
